package org.obeonetwork.dsl.spem.extension.ifest.actions;

import ifest.ontology.SPEMOntologyFactory;
import ifest.ontology.OWLIMTool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.dialogs.ListDialog;
import org.obeonetwork.dsl.spem.MethodContentPackage;
import org.obeonetwork.dsl.spem.MethodContentPackageableElement;
import org.obeonetwork.dsl.spem.MethodPlugin;
import org.obeonetwork.dsl.spem.extension.spem_ifest.Artifact;
import org.obeonetwork.dsl.spem.extension.spem_ifest.ArtifactDefinition;
import org.obeonetwork.dsl.spem.extension.spem_ifest.ArtifactDefinitionPackage;
import org.obeonetwork.dsl.spem.extension.spem_ifest.EngineeringDomain;
import org.obeonetwork.dsl.spem.extension.spem_ifest.EngineeringDomainPackage;
import org.obeonetwork.dsl.spem.extension.spem_ifest.impl.Spem_ifestFactoryImpl;
import org.obeonetwork.dsl.spem.impl.SpemFactoryImpl;
import org.obeonetwork.dsl.spem.uma.CategoryPackage;
import org.obeonetwork.dsl.spem.uma.DisciplinePackage;
import org.obeonetwork.dsl.spem.uma.DomainPackage;
import org.obeonetwork.dsl.spem.uma.GuidancePackage;
import org.obeonetwork.dsl.spem.uma.QualificationPackage;
import org.obeonetwork.dsl.spem.uma.RoleDefinitionPackage;
import org.obeonetwork.dsl.spem.uma.RoleSetPackage;
import org.obeonetwork.dsl.spem.uma.TaskDefinitionPackage;
import org.obeonetwork.dsl.spem.uma.ToolDefinitionPackage;
import org.obeonetwork.dsl.spem.uma.WorkProductDefinitionPackage;
import org.obeonetwork.dsl.spem.uma.WorkProductKindPackage;
import org.obeonetwork.dsl.spem.uma.impl.UmaFactoryImpl;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.xml.sax.SAXException;

import fr.obeo.dsl.viewpoint.tools.api.ui.IExternalJavaAction;

public class ImportMethodContentAction implements IExternalJavaAction{

	MethodPlugin element=null;
	public boolean canExecute(Collection<? extends EObject> arg0) {
		// TODO Auto-generated method stub
		if (arg0.size() != 1) return false;
		EObject o = arg0.iterator().next();		
		if (! (o instanceof MethodPlugin)) return false;
		element = (MethodPlugin)o;
	
		return true;
	}

	public void execute(Collection<? extends EObject> selections,
			Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		//get context list from ontology repository
		List<String> lstContext = OWLIMTool.getOntologyList();
		//get active windows
		Shell page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		//create dialog
		//SectionDia
		ElementListSelectionDialog dlg = new ElementListSelectionDialog(page, new LabelProvider());
		dlg.setElements(lstContext.toArray());
		dlg.setTitle("Choose a context to import");
		dlg.create();
		// TODO Auto-generated method stub
		if (dlg.open()==Window.OK)
		{
			String select_context = (String)dlg.getResult()[0];
			List<String> result = OWLIMTool.getOWL(select_context);
			ListDialog dlg1 = new ListDialog(dlg.getShell());
			dlg1.setAddCancelButton(true);
			
			dlg1.setContentProvider(new ArrayContentProvider());
			
			dlg1.setLabelProvider(new LabelProvider());
			dlg1.setInput(result.toArray());
			dlg1.setTitle("Preview Elements");
			dlg1.create();
			if (dlg1.open()==Window.OK)
			{
				InputDialog dlg2 = new InputDialog(dlg.getShell(), "Naming new method content package dialog", "Give a name for your new package", "Example: imported package", null);
				//dlg2.open();
				if (dlg2.open()==Window.OK)
				{
					String name = dlg2.getValue();
					MethodContentPackage new_package;
					try {
						try {
							new_package = createNewMethodContentPackge(select_context,name);
							element.getOwnedMethodContentPackage().add(new_package);
							MessageBox mssbox = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
							mssbox.setMessage("Successfully");
							mssbox.open();
						} catch (SAXException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ParserConfigurationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					} catch (OWLOntologyCreationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
	protected MethodContentPackage createNewMethodContentPackge(String uri, String package_name) throws OWLOntologyCreationException, SAXException, IOException, ParserConfigurationException
	{
		Spem_ifestFactoryImpl ifestFactory = new Spem_ifestFactoryImpl();
		SpemFactoryImpl spemFactory = new SpemFactoryImpl();
		UmaFactoryImpl umaFactory = new UmaFactoryImpl();
		//createPackage
		MethodContentPackage mp = spemFactory.createMethodContentPackage();
		mp.setName(package_name);
		RoleDefinitionPackage rdp = umaFactory.createRoleDefinitionPackage();
		rdp.setName("Roles");
		mp.getOwnedMethodContentMember().add(rdp);
		TaskDefinitionPackage tdp = umaFactory.createTaskDefinitionPackage();
		tdp.setName("Tasks");
		mp.getOwnedMethodContentMember().add(tdp);
		WorkProductDefinitionPackage wdp = umaFactory.createWorkProductDefinitionPackage();
		wdp.setName("Work Products");
		mp.getOwnedMethodContentMember().add(wdp);
		GuidancePackage gp = umaFactory.createGuidancePackage();
		gp.setName("Guidances");
		mp.getOwnedMethodContentMember().add(gp);
		//ArtifactDefinition
		ArtifactDefinitionPackage adp = ifestFactory.createArtifactDefinitionPackage();
		adp.setName("ArtifactDefinitions");
		gp.getOwnedMethodContentMember().add(adp);
		//
		CategoryPackage standard = umaFactory.createCategoryPackage();
		standard.setName("Standard Categories");
		mp.getOwnedMethodContentMember().add(standard);
		CategoryPackage custom = umaFactory.createCategoryPackage();
		custom.setName("Custom Categories");
		mp.getOwnedMethodContentMember().add(custom);
		DisciplinePackage dp = umaFactory.createDisciplinePackage();
		dp.setName("Disciplines");
		standard.getOwnedMethodContentMember().add(dp);
		DomainPackage domP = umaFactory.createDomainPackage();
		domP.setName("Domains");
		standard.getOwnedMethodContentMember().add(domP);
		WorkProductKindPackage wkp = umaFactory.createWorkProductKindPackage();
		wkp.setName("Work Product Kinds");
		standard.getOwnedMethodContentMember().add(wkp);
		QualificationPackage qp = umaFactory.createQualificationPackage();
		qp.setName("Qualifications");
		standard.getOwnedMethodContentMember().add(qp);
		RoleSetPackage rsp = umaFactory.createRoleSetPackage();
		rsp.setName("Role Sets");
		standard.getOwnedMethodContentMember().add(rsp);
		ToolDefinitionPackage tooldp = umaFactory.createToolDefinitionPackage();
		tooldp.setName("Tools");
		standard.getOwnedMethodContentMember().add(tooldp);
		EngineeringDomainPackage engineeringdomainPkg = ifestFactory.createEngineeringDomainPackage();
		standard.getOwnedMethodContentMember().add(engineeringdomainPkg);
		//get method content
		
		ifest.ontology.SPEMOntologyFactory factory = new SPEMOntologyFactory();
		String repository = "D:/Working/xml/repositoryInfo.xml";
		String ifest = "D:/Working/xml/ifestInfo.xml";
		factory.init(repository, ifest);
		factory.getOntologyContent(uri);
		engineeringdomainPkg.getOwnedMethodContentMember().addAll(factory.getEngineeringDomain());
		List<EngineeringDomain> engineeringDomains = new ArrayList<EngineeringDomain>();
		for (MethodContentPackageableElement e: engineeringdomainPkg.getOwnedMethodContentMember())
		{
			if (e instanceof EngineeringDomain)
			{
				engineeringDomains.add((EngineeringDomain) e);
			}
		}
		//generate spem tool definition
		tooldp.getOwnedMethodContentMember().addAll(factory.getToolTypes(uri,engineeringDomains));

		//generate spem artifact definition
		List<ArtifactDefinition> listAD = factory.getArtifactDefinition( engineeringDomains);
		adp.getOwnedMethodContentMember().addAll(listAD);
		//generate spem artifact
		List<Artifact> lstArtifact = factory.getArtifact(listAD);
		wdp.getOwnedMethodContentMember().addAll(lstArtifact);
		//generate spem task definition
				tdp.getOwnedMethodContentMember().addAll(factory.getTaskDefinitions(engineeringDomains,lstArtifact));
		return mp;
	}
}
